from pyecharts.charts import Page
import pandas as pd
import sys
sys.path.append("..//database")
from SqlWizard import *
from CmnFuncs import *

def run_other_alloy():
    '''
    本模块包含品种：硅铁，锰硅，不锈钢现货主连，关联品种：镍
    设计图表：
    1. 硅铁现货基差图；
    2. 硅铁月差图；
    3. 锰硅现货基差图；
    4. 锰硅月差图；
    5. 硅铁 / 锰硅 比价
    6. 不锈钢现货基差图；
    7. 镍现货基差图；
    8. 不锈钢 / 镍 现货及主连比价
    '''

    a_list = date_checker_1()

    sf_spot = read_sql_spot('spot_sf')
    sf_index = read_sql_fut_index('fut_index_sf')
    sf_ry = read_sql_ry('roll_yield_sf')
    sf_spdrdx = read_sql_spdrdx('spdrdex_sf')
    sf_1 = read_sql_fut('fut_sf{}'.format(a_list[0]))
    sf_2 = read_sql_fut('fut_sf{}'.format(a_list[1]))
    sf_3 = read_sql_fut('fut_sf{}'.format(a_list[2]))

    sm_spot = read_sql_spot('spot_sm')
    sm_index = read_sql_fut_index('fut_index_sm')
    sm_ry = read_sql_ry('roll_yield_sm')
    sm_spdrdx = read_sql_spdrdx('spdrdex_sm')
    sm_1 = read_sql_fut('fut_sm{}'.format(a_list[0]))
    sm_2 = read_sql_fut('fut_sm{}'.format(a_list[1]))
    sm_3 = read_sql_fut('fut_sm{}'.format(a_list[2]))

    ss_spot = read_sql_spot('spot_ss')
    ss_index = read_sql_fut_index('fut_index_ss')
    ss_ry = read_sql_ry('roll_yield_ss')
    ss_spdrdx = read_sql_spdrdx('spdrdex_ss')
    ss_1 = read_sql_fut('fut_ss{}'.format(a_list[0]))
    ss_2 = read_sql_fut('fut_ss{}'.format(a_list[1]))
    ss_3 = read_sql_fut('fut_ss{}'.format(a_list[2]))

    ni_spot = read_sql_spot('spot_ni')
    ni_index = read_sql_fut_index('fut_index_ni')

    # 硅铁
    header_sf = draw_separator('硅铁')
    # 硅铁远期曲线
    future_curve_sf = draw_future_curve(sf_spot, sf_1, sf_2, sf_3)
    # 硅铁一致性预期指标
    idctr_sf = draw_spec_idctr('硅铁', sf_spdrdx, sf_index, sf_ry)
    # 1. 硅铁现货基差图；
    sf_spot_chart = draw_spot_spd('硅铁', sf_spot)
    # 硅铁合约价格、成交量、持仓量、持仓量变化图
    sf_contract_1 = draw_contract('sf{}'.format(a_list[0]), sf_1)
    sf_contract_2 = draw_contract('sf{}'.format(a_list[1]), sf_2)
    sf_contract_3 = draw_contract('sf{}'.format(a_list[2]), sf_3)
    # 2. 硅铁月差图；
    sf_12_chart = draw_spd('sf{}'.format(a_list[0]), sf_1['close'], 'sf{}'.format(a_list[1]), sf_2['close'])
    sf_23_chart = draw_spd('sf{}'.format(a_list[1]), sf_2['close'], 'sf{}'.format(a_list[2]), sf_3['close'])

    # 锰硅
    header_sm = draw_separator('锰硅')
    # 锰硅远期曲线
    future_curve_sm = draw_future_curve(sm_spot, sm_1, sm_2, sm_3)
    # 锰硅一致性预期指标
    idctr_sm = draw_spec_idctr('锰硅', sm_spdrdx, sm_index, sm_ry)
    # 3. 锰硅现货基差图；
    sm_spot_chart = draw_spot_spd('锰硅', sm_spot)
    # 锰硅合约价格、成交量、持仓量、持仓量变化图
    sm_contract_1 = draw_contract('sm{}'.format(a_list[0]), sm_1)
    sm_contract_2 = draw_contract('sm{}'.format(a_list[1]), sm_2)
    sm_contract_3 = draw_contract('sm{}'.format(a_list[2]), sm_3)
    # 4. 锰硅月差图；
    sm_12_chart = draw_spd('sm{}'.format(a_list[0]), sm_1['close'], 'sm{}'.format(a_list[1]), sm_2['close'])
    sm_23_chart = draw_spd('sm{}'.format(a_list[1]), sm_2['close'], 'sm{}'.format(a_list[2]), sm_3['close'])

    # 不锈钢
    header_ss = draw_separator('不锈钢')
    # 不锈钢远期曲线
    future_curve_ss = draw_future_curve(ss_spot, ss_1, ss_2, ss_3)
    # 不锈钢一致性预期指标
    idctr_ss = draw_spec_idctr('不锈钢', ss_spdrdx, ss_index, ss_ry)
    # 不锈钢现货基差图；
    ss_spot_chart = draw_spot_spd('不锈钢', ss_spot)
    # 不锈钢合约价格、成交量、持仓量、持仓量变化图
    ss_contract_1 = draw_contract('ss{}'.format(a_list[0]), ss_1)
    ss_contract_2 = draw_contract('ss{}'.format(a_list[1]), ss_2)
    ss_contract_3 = draw_contract('ss{}'.format(a_list[2]), ss_3)
    # 不锈钢月差图；
    ss_12_chart = draw_spd('ss{}'.format(a_list[0]), ss_1['close'], 'ss{}'.format(a_list[1]), ss_2['close'])
    ss_23_chart = draw_spd('ss{}'.format(a_list[1]), ss_2['close'], 'ss{}'.format(a_list[2]), ss_3['close'])

    # 品种间
    header_cross = draw_separator('品种间')
    # 5. 硅铁 / 锰硅 比价
    arbi_record('sf', sf_index['close'], 'sm', sm_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('sf', sf_spot['spot'], 'sm', sm_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_sf_sm_spot = draw_price_ratio('硅铁现货', sf_spot['spot'], '锰硅现货', sm_spot['spot'], 1)
    ratio_sf_sm_main = draw_price_ratio('硅铁主连', sf_spot['main_price'], '锰硅主连', sm_spot['main_price'], 1)
    ratio_sf_sm_1 = draw_price_ratio('sf{}'.format(a_list[0]), sf_1['close'], 'sm{}'.format(a_list[0]), sm_1['close'], 1)
    ratio_sf_sm_2 = draw_price_ratio('sf{}'.format(a_list[1]), sf_2['close'], 'sm{}'.format(a_list[1]), sm_2['close'], 1)
    ratio_sf_sm_3 = draw_price_ratio('sf{}'.format(a_list[2]), sf_3['close'], 'sm{}'.format(a_list[2]), sm_3['close'], 1)

    # 8. 不锈钢 / 镍 现货及主连比价
    arbi_record('ss', ss_index['close'], 'ni', ni_index['close'], x=1, y=1, opt=1, cat=1)
    arbi_record('ss', ss_spot['spot'], 'ni', ni_spot['spot'], x=1, y=1, opt=1, cat=2)
    ratio_ss_ni_spot = draw_price_ratio('不锈钢现货', ss_spot['spot'], '镍现货', ni_spot['spot'], 3)
    ratio_ss_ni_main = draw_price_ratio('不锈钢主连', ss_spot['main_price'], '镍主连', ni_spot['main_price'], 3)

    page = Page(page_title='其他合金', layout=Page.SimplePageLayout)
    page.add(
        header_sf,
        future_curve_sf,
        idctr_sf,
        sf_spot_chart,
        sf_contract_1,
        sf_contract_2,
        sf_contract_3,
        sf_12_chart,
        sf_23_chart,
        header_sm,
        future_curve_sm,
        idctr_sm,
        sm_spot_chart,
        sm_contract_1,
        sm_contract_2,
        sm_contract_3,
        sm_12_chart,
        sm_23_chart,
        header_ss,
        future_curve_ss,
        idctr_ss,
        ss_spot_chart,
        ss_contract_1,
        ss_contract_2,
        ss_contract_3,
        ss_12_chart,
        ss_23_chart,
        header_cross,
        ratio_sf_sm_spot,
        ratio_sf_sm_main,
        ratio_sf_sm_1,
        ratio_sf_sm_2,
        ratio_sf_sm_3,
        ratio_ss_ni_spot,
        ratio_ss_ni_main,
    )
    page.render('其他合金.html')

